package cn.regexp.demo.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.regexp.demo.entity.University;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Regexpei
 * @date 2024/6/23 16:52
 * @description 大学数据工具类
 */
public class UniversityDataUtils {

    private UniversityDataUtils() {
    }

    /**
     * 每页条数
     */
    public static Integer pageSize = 10;

    /**
     * 页码
     */
    public static Integer pageNo = 0;

    /**
     * 获取下一页数据
     *
     * @return 大学数据列表
     */
    public static List<University> getNextPage() {
        String result = HttpUtil.get("https://static-data.gaokao.cn/www/2.0/school/list_v2.json");
        String dataStr = JSONUtil.parseObj(result).getStr("data");
        JSONObject data = JSONUtil.parseObj(dataStr);
        List<University> universityList = data.keySet().parallelStream().map(id -> {
            JSONObject object = data.getJSONObject(id);
            University university = new University();
            university.setId(id);
            university.setName(object.getStr("name"));
            university.setProvince(object.getStr("p"));
            university.setCity(object.getStr("c"));
            university.setF985(object.getInt("f985"));
            university.setF211(object.getInt("f211"));
            university.setNature(object.getStr("nature"));
            return university;
        }).collect(Collectors.toList());

        pageNo++;
        return PaginationHelper.paginate(universityList, pageNo, pageSize);
    }

}
